package com.aglhz.nature.view;



import java.util.LinkedList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.aglhz.nature.R;
import com.aglhz.nature.utils.Constants;
import com.aglhz.nature.utils.MD5;
import com.aglhz.nature.utils.Util;
import com.tencent.mm.sdk.constants.Build;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class PayActivity extends Activity {
	
	private IWXAPI api;
	private String payInfo;
	private String appId;
	private String partnerId;
	private String prepayId;
	private String packageValue;
	private String nonceStr;
	private String timeStamp;
	private JSONObject json;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay);
		
		api = WXAPIFactory.createWXAPI(this, "wxdbb86823bfceeb3d");
		Intent intent=getIntent();		
		payInfo=intent.getStringExtra("payInfo");
		Log.e("payInfo",payInfo);
		
		Toast.makeText(PayActivity.this, "获取订单中...", Toast.LENGTH_SHORT).show();
		
	
			try {
				json = new JSONObject(payInfo);
				JSONObject payData = json.getJSONObject("data");
				Log.e("json",json.toString());
				Log.e("payData",payData.toString());
				
				if(null != json && !json.has("retcode") ){
					PayReq req = new PayReq();
					
					req.appId =payData.getString("appId");
					Log.e("appId",req.appId);
					req.partnerId = payData.getString("partnerid");
					Log.e("partnerId",req.partnerId);
					req.prepayId=  payData.getString("prepayid");
					Log.e("prepayId",req.prepayId);
					req.packageValue = payData.getString("package_");
					Log.e("packageValue",req.packageValue);
					req.nonceStr=  payData.getString("noncestr");
					Log.e("nonceStr",req.nonceStr);
					req.timeStamp= payData.getString("timeStamp");
					Log.e("timeStamp",req.timeStamp);
					// req.sign= payData.getString("sign");
					// Log.e("sign",req.sign);
					
					List<NameValuePair> signParams = new LinkedList<NameValuePair>();
		            signParams.add(new BasicNameValuePair("appid", req.appId));
					signParams.add(new BasicNameValuePair("noncestr", req.nonceStr));
					signParams.add(new BasicNameValuePair("package", req.packageValue));
					signParams.add(new BasicNameValuePair("partnerid", req.partnerId));
					signParams.add(new BasicNameValuePair("prepayid", req.prepayId));
					signParams.add(new BasicNameValuePair("timestamp", req.timeStamp));
					
					req.sign = genAppSign(signParams);
					
					
					Toast.makeText(PayActivity.this, "正常调起支付", Toast.LENGTH_SHORT).show();
					// 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
//					api.sendReq(req);
					
				}else{
		        	Log.d("PAY_GET", "返回错误"+json.getString("retmsg"));
		        	Toast.makeText(PayActivity.this, "返回错误"+json.getString("retmsg"), Toast.LENGTH_SHORT).show();
						}
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			
				
				
			}
			
			
			
		
	}
	
	// 二次签名
	private String genAppSign(List<NameValuePair> params) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < params.size(); i++) {
			sb.append(params.get(i).getName());
			sb.append('=');
			sb.append(params.get(i).getValue());
			sb.append('&');
		}
		sb.append("key=");
		sb.append(Constants.API_KEY);

		String appSign = MD5.getMessageDigest(sb.toString().getBytes()).toUpperCase();
	
		return appSign;
	}
	
	
	


}
	
	

	


